1
|
|
|
var inherits = require('../src/inherits.js'); |
2
|
|
|
|
3
|
|
|
describe('inherits', function() { |
4
|
|
|
describe('failing tests', function() { |
5
|
|
|
it("should fail because the Constructor is undefined", function() { |
6
|
|
|
expect(inherits.bind()).toThrowError(TypeError); |
7
|
|
|
}); |
8
|
|
|
|
9
|
|
|
it("should fail because the Constructor is null", function() { |
10
|
|
|
expect(inherits.bind(null, null)).toThrowError(TypeError); |
11
|
|
|
}); |
12
|
|
|
|
13
|
|
|
it("should fail because the SuperConstructor is undefined", function() { |
14
|
|
|
expect(inherits.bind(null, function() {})).toThrowError(TypeError); |
15
|
|
|
}); |
16
|
|
|
|
17
|
|
|
it("should fail because the SuperConstructor is null", function() { |
18
|
|
|
expect(inherits.bind(null, function() {}, null)).toThrowError(TypeError); |
19
|
|
|
}); |
20
|
|
|
|
21
|
|
|
it("should fail because the SuperConstructor has no prototype", function() { |
22
|
|
|
expect(inherits.bind(null, function() {}, [])).toThrowError(TypeError); |
23
|
|
|
}); |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
describe('successful tests', function() { |
27
|
|
|
// test example from README.md |
28
|
|
|
var SuperClass = function() { |
29
|
|
|
this.someProperty = 42; |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
SuperClass.prototype.retMsg = function(msg) { |
33
|
|
|
return msg; |
34
|
|
|
}; |
35
|
|
|
|
36
|
|
|
SuperClass.prototype.getSomeProp = function() { |
37
|
|
|
return this.someProperty; |
38
|
|
|
}; |
39
|
|
|
|
40
|
|
|
// a class we want to inherit from SuperClass |
41
|
|
|
var SomeClass = function() { |
42
|
|
|
SuperClass.call(this); |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
inherits(SomeClass, SuperClass); |
46
|
|
|
|
47
|
|
|
SomeClass.prototype.flop = function(msg) { |
48
|
|
|
return this.retMsg(msg); |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
var someClass_instance = new SomeClass(); |
52
|
|
|
// end test example from README.md |
53
|
|
|
|
54
|
|
|
it("should be able to access inherited properties", function() { |
55
|
|
|
expect(someClass_instance.someProperty).toEqual(42); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
it("should be able to run inherited methods", function() { |
59
|
|
|
expect(someClass_instance.retMsg('hi')).toEqual('hi'); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
it("should be able to run inherited methods by using the this keyword", function() { |
63
|
|
|
expect(someClass_instance.flop('hi')).toEqual('hi'); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
it("should be instance of SomeClass", function() { |
67
|
|
|
expect((someClass_instance instanceof SomeClass)).toEqual(true); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
it("should be instance of SuperClass", function() { |
71
|
|
|
expect((someClass_instance instanceof SuperClass)).toEqual(true); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
it("should set SuperClass contructor", function() { |
75
|
|
|
expect(someClass_instance.constructor.super_ === SuperClass).toEqual(true); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
it("should set contructor", function() { |
79
|
|
|
expect(someClass_instance.constructor === SomeClass).toEqual(true); |
80
|
|
|
}); |
81
|
|
|
}); |
82
|
|
|
}); |
83
|
|
|
|